package com.wumai.keystrokes.events;

import com.wumai.keystrokes.models.display.Key;
import com.wumai.keystrokes.renders.RenderKeyOnScreen;
import com.wumai.keystrokes.utils.KeyConfigManger;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.HashMap;
import java.util.Map;

/**
 * @author trick12138
 * @date 2025-08-20
 * @description 事件注册
 */
@Mod.EventBusSubscriber()
public class ScreenKeyEvents {
    // 存储按键状态（按下/抬起）
    private static final Map<Integer, Boolean> KeyboardStates = new HashMap<>();
    private static final Map<Integer, Boolean> mouseStates = new HashMap<>();

    @SubscribeEvent
    public static void onOverlayRender(RenderGameOverlayEvent event) {
        if (event.getType() != RenderGameOverlayEvent.ElementType.ALL) {
            return;
        }
        if (Minecraft.getInstance().player == null) {
            return;
        }
        
        // 检查是否有当前配置
        if (KeyConfigManger.getCurrentGroup() == null) {
            return;
        }

        RenderKeyOnScreen renderKeyOnScreen = new RenderKeyOnScreen(event.getMatrixStack());

        // 渲染当前按键组中的所有按键
        for (Key key : KeyConfigManger.getCurrentGroup().getKeys()) {
            if (key.getKeyType() == Key.KeyType.KEYBOARD) {
                if (KeyboardStates.getOrDefault(key.getKeyCode(), false)) {
                    renderKeyOnScreen.renderKeyOnScreen(
                            key.getPressedResource(),
                            key.getPosition(),
                            key.getWidth(),
                            key.getHeight()
                    );
                } else {
                    renderKeyOnScreen.renderKeyOnScreen(
                            key.getReleasedResource(),
                            key.getPosition(),
                            key.getWidth(),
                            key.getHeight()
                    );
                }
            } else {
                if (mouseStates.getOrDefault(key.getKeyCode(), false)) {
                    renderKeyOnScreen.renderKeyOnScreen(
                            key.getPressedResource(),
                            key.getPosition(),
                            key.getWidth(),
                            key.getHeight()
                    );
                } else {
                    renderKeyOnScreen.renderKeyOnScreen(
                            key.getReleasedResource(),
                            key.getPosition(),
                            key.getWidth(),
                            key.getHeight()
                    );
                }
            }

        }
    }
    
    @SubscribeEvent
    public static void onKeyPressed(InputEvent.KeyInputEvent event) {
        // 更新按键状态
        if (event.getAction() == 1) { // 按下
            KeyboardStates.put(event.getKey(), true);
        } else if (event.getAction() == 0) { // 抬起
            KeyboardStates.put(event.getKey(), false);
        }
    }
    
    @SubscribeEvent
    public static void onMouseClicked(InputEvent.MouseInputEvent event) {
        int button = event.getButton();
        if (event.getAction() == 1) { // 按下
            mouseStates.put(button, true); // 使用负数区分鼠标和键盘按键
        } else if (event.getAction() == 0) { // 抬起
            mouseStates.put(button, false);
        }
    }
}